Imports ConnectionServer
Imports System.Configuration
Imports System.Web
Imports System.Web.Caching
Imports System.Web.Configuration
Imports System.Data.SqlClient
Imports System.Collections.Specialized
Namespace Utility
    Public Class Settings
        Inherits StringDictionary
        Private meDatabase As Connection.SourceDataBase

        Private msCacheKey As String = String.Empty
        Private msTable As String = String.Empty
        Private msName As String = String.Empty
        Private msValue As String = String.Empty
        Private Const APP_KEYS As String = "AppKeys"
        Private msCountry As String = String.Empty

        Public Sub New()

            msCacheKey = APP_KEYS
            LoadCache(String.Empty)

        End Sub

        Public Function GetSetting(ByVal psSetting As String) As String
            Return Item(psSetting)
        End Function

        Public Sub New(ByVal peDatabase As Connection.SourceDataBase)
            meDatabase = peDatabase
            msCacheKey = peDatabase.ToString
            LoadCache(String.Empty)

        End Sub

        <Obsolete("Do not Use", True)> Public Sub New(ByVal psSetting As String)
            Add(psSetting, ConfigurationManager.AppSettings(psSetting).ToString)
        End Sub

        Private Sub LoadCache(ByVal psCountry As String)
            msCountry = psCountry
            Dim loRT As HttpRuntime = New HttpRuntime
            Dim lsCh As StringDictionary = CType(HttpRuntime.Cache.Item("Settings" & msCacheKey & msCountry), StringDictionary)
            If lsCh Is Nothing Then
                Refresh(psCountry)
            Else
                For Each loItem As DictionaryEntry In lsCh
                    Me.Add(loItem.Key.ToString, loItem.Value.ToString)
                Next
            End If
        End Sub

        Public Sub New(ByVal peDatabase As Connection.SourceDataBase, ByVal psCountry As String)

            meDatabase = peDatabase
            msCacheKey = peDatabase.ToString
            LoadCache(psCountry)

        End Sub

        Public Sub Refresh(ByVal psCountry As String)

            If msCacheKey = APP_KEYS Then
                'If its from AppKeys then load from App Keys
                For Each lsSetting As String In ConfigurationManager.AppSettings.Keys
                    Add(lsSetting, ConfigurationManager.AppSettings(lsSetting).ToString)
                Next
            Else
                'Otherwise from the database
                Dim moConnection As Connection = New Connection(meDatabase)
                Using moConn As SqlConnection = moConnection.Connection
                    Dim loReader As SqlDataReader
                    Me.Clear()
                    Dim lsSql As String = String.Empty
                    lsSql = "SELECT * FROM CMT_Settings"
                    Using loComm As SqlCommand = New SqlCommand(lsSql, moConn)
                        loReader = loComm.ExecuteReader(CommandBehavior.CloseConnection)
                        Do While loReader.Read
                            Add(loReader("Name").ToString, CStr(loReader("Value")))
                        Loop
                        loReader.Close()
                    End Using


                End Using
            End If
        End Sub
        Public Sub Save()
            Dim loConnectionServer As Connection = New Connection(Connection.SourceDataBase.CMT)
            Dim loConn As SqlConnection = loConnectionServer.Connection
            Dim lsKey As String
            For Each lsKey In Keys
                Dim loComm As SqlCommand = New SqlCommand("EXEC dbo.Setting_Save '" & lsKey & "','" & Item(lsKey) & "'", loConn)
                loComm.ExecuteNonQuery()
            Next
            LoadCache(String.Empty)
        End Sub

        Public Overrides Sub Add(ByVal key As String, ByVal value As String)
            If ContainsKey(key) Then
                If Not ConfigurationManager.AppSettings(key) Is Nothing Then
                    Item(key) = ConfigurationManager.AppSettings(key).ToString
                Else
                    Item(key) = value
                End If

            Else
                If Not ConfigurationManager.AppSettings(key) Is Nothing Then
                    MyBase.Add(key, ConfigurationManager.AppSettings(key).ToString)
                Else
                    MyBase.Add(key, value)
                End If

            End If
        End Sub

        Protected Overrides Sub Finalize()
            Dim loRT As HttpRuntime = New HttpRuntime
            HttpRuntime.Cache.Add("Settings" & msCacheKey & msCountry, Me, Nothing, Date.MaxValue, New TimeSpan(1, 0, 0, 0), CacheItemPriority.Default, Nothing)
            'Save to Cache with the CacheKey
            MyBase.Finalize()
        End Sub

        Public Function GetKeyValue(ByVal strKey As String) As String
            Dim settings As New Settings()
            Dim settingValue As String
            settingValue = String.Empty
            ' Check if the value is present in the cache
            If HttpRuntime.Cache("Settings") Is Nothing Then
                ' Check if the string value is present in the app config section
                Dim appSettings As NameValueCollection = ConfigurationManager.AppSettings
                If appSettings.Item(strKey) Is Nothing Then
                    ' Get the value from database
                    settings = New Settings(Connection.SourceDataBase.CMT)
                    settingValue = settings(strKey)
                Else
                    settingValue = appSettings.GetValues(strKey)(0)
                End If
            Else
                settings = CType(HttpRuntime.Cache.Item("Settings"), Utility.Settings)
                settingValue = settings(strKey)
            End If
            Return settingValue
        End Function
    End Class
End Namespace
